﻿using BussinessLogic;
using DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WSS.hameduconn.Bases;

namespace WSS.hameduconn
{
    public partial class Register : BasePage
    {
        BLUser userControls;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                divAlert.Visible = false;
        }

        protected void AlertWarning(string msg)
        {
            divAlert.Attributes["class"] = "alert alert-danger";
            divAlert.InnerHtml = msg;
            divAlert.Visible = true;
        }

        protected void AlertError(string msg)
        {
            divAlert.Attributes["class"] = "alert alert-danger";
            divAlert.InnerHtml = msg;
            divAlert.Visible = true;
        }

        protected void AlertSuccess(string msg)
        {
            divAlert.Attributes["class"] = "alert alert-success";
            divAlert.InnerHtml = msg;
            divAlert.Visible = true;
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            userControls = new BLUser();
            string city = tbCity.Text;
            string creditCard = tbCreditNo.Text;
            string cvs = tbCVS.Text;
            string email = tbEmail.Text;
            string expDate = tbExpirationDate.Text;
            string fName = tbFirstName.Text;
            string lName = tbLastName.Text;
            string passWord = tbPassword.Text;
            string confirmPassWord = tbConfirmPassword.Text;
            string state = tbState.Text; string street = tbStreet.Text;
            string username = tbUsername.Text;
            string zipCode = tbZipcode.Text;

            if (string.IsNullOrEmpty(username))
            {
                AlertWarning("Please input require field");
                return;
            }

            if (passWord != confirmPassWord)
            {
                AlertWarning("Confirm password incorrect");
                return;
            }

            if (!string.IsNullOrEmpty(email) && !Utilities.IsValidEmail(email))
            {
                AlertWarning("Wrong email syntax");
                return;
            }

            if (!userControls.CheckUser(username))
            {
                AlertWarning("Username is exist");
                return;
            }

            DateTime? date = null;
            User user = new User
            {
                City = city,
                CreditCardNo = creditCard,
                CVS = cvs,
                Email = email,
                ExpDate = string.IsNullOrEmpty(expDate) ? date : DateTime.Parse(expDate),
                Firstname = fName,
                Lastname = lName,
                Password = new AUEncrypt().Encrypt(passWord),
                State = state,
                Street = street,
                Username = username,
                Zipcode = zipCode,
            };

            if (userControls.CreatUser(ref user))
            {
                Session["OAuth"] = new Authenticate { admin = null, user = user };
                Response.Redirect(ResolveUrl("~/Profile.aspx"));
            }
            else
            {
                AlertError("Create new user fail!");
            }
        }
    }
}